package com.kinyx;

import com.kinyx.demo.entities.impl1.DemoSysUserEntity;
import com.kinyx.demo.entities.impl1.DemoSysUserEntityR;
import com.kinyx.framework.business.KRandomUtils;
import com.kinyx.framework.db.configs.DBConfigsHelper;
import com.kinyx.framework.db.daos.JDBCDao;
import com.kinyx.framework.db.daos.JDBCDaoPrimary;
import com.kinyx.framework.db.sentence.res.Condition;
import com.kinyx.framework.db.sentence.res.KDELETE;
import com.kinyx.framework.db.sentence.res.KINSERT;
import com.kinyx.framework.db.sentence.res.KSQLBuilder;
import com.kinyx.framework.db.sentence.res.KUPDATE;
import com.kinyx.framework.enums.impl.RandomRange;
import com.kinyx.framework.enums.impl.UsingStatus;
import com.kinyx.framework.jexl.KRandomUtils2;
import com.kinyx.framework.log.Log;
import com.kinyx.framework.log.Logger;
import com.kinyx.framework.utils.KDateTimeUtils;
import com.kinyx.framework.utils.KStringUtils;

public class KMain {

	/** 日志对象 */
	private static final Log LOGGER = Logger.getLog(KMain.class);

	public static void main(final String[] args) {
		LOGGER.info("main");
		final JDBCDao dao = new JDBCDaoPrimary();

		clear(dao);

		change(dao);

		save_1(dao);
		save_2(dao);

		delete_1(dao);
		delete_2(dao);
		delete_3(dao);

		datas(dao);
	}

	private static String userCard() { return KRandomUtils.randomString(false, KRandomUtils.random(64, 64), RandomRange.NUMBER_AND_LETTER_FULL); }
	private static String userName() { return KRandomUtils.randomString(false, KRandomUtils.random(3, 16), RandomRange.LETTER_FULL); }
	private static String birthday() { return KRandomUtils.randomBoolean(false) ? null : (KRandomUtils.random(1986, 2018) + "-" + KStringUtils.fillZero(KRandomUtils.random(1, 12), 2) + "-" + KStringUtils.fillZero(KRandomUtils.random(1, 28), 2)); }
	private static String cellphone() { return String.valueOf(KRandomUtils.random(13000000000L, 19999999999L)); }
	private static String email() { return KRandomUtils.randomBoolean(false) ? null : (KRandomUtils.randomString(false, KRandomUtils.random(3, 16), RandomRange.LETTER_FULL) + "@" + KRandomUtils.randomString(false, KRandomUtils.random(3, 16), RandomRange.NUMBER_AND_LETTER_FULL) + ".com"); }

	private static void change(final JDBCDao dao) {
		final String now = KDateTimeUtils.now("yyyyMMddHHmmssSSS");

		final String id1 = "1";
		final String id2 = "2";

		LOGGER.debug("insert using res.INSERT");
		final KINSERT insert = KSQLBuilder.insert(DemoSysUserEntityR.class)//
				.values(DemoSysUserEntityR.id, id1)//
				.values(DemoSysUserEntityR.userCard, userCard())//
				.values(DemoSysUserEntityR.userName, userName())//
				.values(DemoSysUserEntityR.birthday, birthday())//
				.values(DemoSysUserEntityR.cellphone, cellphone())//
				.values(DemoSysUserEntityR.email, email())//
				.values(DemoSysUserEntityR.usingStatus, UsingStatus.USING)//
				.values(DemoSysUserEntityR.insertTime, now)//
				.values(DemoSysUserEntityR.updateTime, now);
		final int cnt1 = dao.insert(null, insert);
		LOGGER.info("insert success, " + cnt1);

		LOGGER.debug("update using res.UPDATE");
		final KUPDATE update = KSQLBuilder.update(DemoSysUserEntityR.class)//
				.set(DemoSysUserEntityR.id, id2)//
				.where(Condition.eq(DemoSysUserEntityR.id, id1));
		final int cnt2 = dao.update(null, update);
		LOGGER.info("update success, " + cnt2);

		LOGGER.debug("delete using res.DELETE");
		final KDELETE delete = KSQLBuilder.delete(DemoSysUserEntityR.class)//
				.where(Condition.eq(DemoSysUserEntityR.id, id2));
		final int cnt3 = dao.delete(null, delete);
		LOGGER.info("delete success, " + cnt3);
	}

	private static void save_1(final JDBCDao dao) {
		LOGGER.debug("save_1");
		LOGGER.debug("save_1 -> 1、主键无值。新增数据。");

		final DemoSysUserEntity entity = new DemoSysUserEntity();
		entity.setUserCard(userCard());
		entity.setUserName(userName());
		entity.setBirthday(birthday());
		entity.setCellphone(cellphone());
		entity.setEmail(email());
		dao.save(null, entity);
	}

	private static void save_2(final JDBCDao dao) {
		LOGGER.debug("save_2_1");
		LOGGER.debug("save_2_1 -> 2、主键有值。按照主键查表。");
		LOGGER.debug("save_2_1 -> 2.1、无数据。新增数据。");

		final DemoSysUserEntity entity = new DemoSysUserEntity();
		entity.setId(DBConfigsHelper.getDBConfigs(dao.getKey()).getPKGenerator().id(32));
		entity.setUserCard(userCard());
		entity.setUserName(userName());
		entity.setBirthday(birthday());
		entity.setCellphone(cellphone());
		entity.setEmail(email());
		dao.save(null, entity);

		LOGGER.debug("save_2_2");
		LOGGER.debug("save_2_2 -> 2、主键有值。按照主键查表。");
		LOGGER.debug("save_2_2 -> 2.2、有数据。校验是否有字段修改。");
		LOGGER.debug("save_2_2 -> 2.2.1、无字段修改。忽略。");
		dao.save(null, entity);

		LOGGER.debug("save_2_3");
		LOGGER.debug("save_2_3 -> 2、主键有值。按照主键查表。");
		LOGGER.debug("save_2_3 -> 2.2、有数据。校验是否有字段修改。");
		LOGGER.debug("save_2_3 -> 2.2.2、有字段修改。更新数据。");
		entity.setUsingStatus(UsingStatus.DEPRECATED);
		dao.save(null, entity);
	}

	private static void delete_1(final JDBCDao dao) {
		LOGGER.debug("delete_1");
		LOGGER.debug("delete_1 -> 1、主键无值。忽略。");
		final DemoSysUserEntity entity = new DemoSysUserEntity();
		dao.remove(null, entity);
	}

	private static void delete_2(final JDBCDao dao) {
		LOGGER.debug("delete_2");
		LOGGER.debug("delete_2 -> 2、主键有值。按照主键查表。");
		LOGGER.debug("delete_2 -> 2.1、无数据。忽略。");
		final DemoSysUserEntity entity = new DemoSysUserEntity();
		entity.setId(DBConfigsHelper.getDBConfigs(dao.getKey()).getPKGenerator().id(32));
		dao.remove(null, entity);
	}

	private static void delete_3(final JDBCDao dao) {
		LOGGER.debug("delete_3");
		LOGGER.debug("delete_3 -> 先存一条数据");
		final DemoSysUserEntity entity = new DemoSysUserEntity();
		entity.setUserCard(userCard());
		entity.setUserName(userName());
		entity.setBirthday(birthday());
		entity.setCellphone(cellphone());
		entity.setEmail(email());
		dao.save(null, entity);
		LOGGER.debug("delete_3 -> 2、主键有值。按照主键查表。");
		LOGGER.debug("delete_3 -> 2.2、有数据。删除数据。");
		dao.remove(null, entity);
	}

	private static void datas(final JDBCDao dao) {
		KRandomUtils2.randomBeanList(DemoSysUserEntity.class, 0, 100, 150).forEach(entity -> {
			dao.save(null, entity);
		});
	}

	private static void clear(final JDBCDao dao) {
		dao.delete(null, KSQLBuilder.delete(DemoSysUserEntityR.class));
	}

}
